<?php

namespace App\Controller\Admin;

use App\Controller\Example\UserAddrExample;
use App\Models\Conn;
use Core\Common\Common;
use Core\Result\Result;

class UsersAddr
{
    /**
     * 管理后台用户收货地址列表
     * entry token,uid,name,mobile,startTime,endTime,page,limit
     * return json
     * */
    public function usersAddrList($data){
        $assemble = Common::assemble(new UserAddrExample(), $data);
        $identical = Common::identical($data);
        $conn = new Conn();
        $joinSql = " WHERE `status` = 1 ";
        if($assemble->uid !=null){ $joinSql.=" and `uid` ='$assemble->uid' ";}
        if($assemble->mobile !=null){ $joinSql.=" and `mobile` ='$assemble->mobile' ";}
        if($assemble->name !=null){ $joinSql.=" and `name` ='$assemble->name' ";}
        if($assemble->startTime !=null){ $joinSql.=" and UNIX_TIMESTAMP(`time`) >= '$assemble->startTime' ";}
        if($assemble->endTime !=null){ $joinSql.=" and UNIX_TIMESTAMP(`time`) <= '$assemble->endTime' ";}
        $sql = "SELECT `id`, `uid`, `mobile`, `name`, `address`, `detailed_address`, `isdefault`,`time`,`status`,`areaids` FROM `users_addr` $joinSql order by id desc limit $identical->pageStart,$identical->limit";
        $readConn = $conn->readConn($sql,true);
        foreach ($readConn as $item=>$value){
            switch ($value["isdefault"]){
                case 1:$readConn[$item]["isdefault"]= "默认";break;
                default:$readConn[$item]["isdefault"]= "普通";break;
            }
        }
        $returnData["list"]=$readConn;
        $countSql="SELECT count(id) as count FROM `users_addr` $joinSql";
        $returnData["count"] = $conn->readConn($countSql)["count"];
        $returnData["total"] = null;
        Result::success($returnData);
    }
}